package com.atguigu.controller;

import com.atguigu.client.SeckillFeignClient;
import com.atguigu.result.RetVal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
 * @Description
 * @Author: TT
 * @Date: 2022/8/12 20:32
 */
@Controller
public class WebSeckillController {
    @Autowired
    private SeckillFeignClient seckillFeignClient;
    //秒杀首页列表页面
    @GetMapping("/seckill-index.html")
    public String seckillIndex(Model model){
        RetVal retVal = seckillFeignClient.queryAllSeckillProduct();
        model.addAttribute("list",retVal.getData());
        return "seckill/index";
    }

    //2.根据skuId 获取秒杀对象数据,秒杀商品详情页面
    @GetMapping("/seckill-detail/{skuId}.html")
    public String seckillDetail(@PathVariable Long skuId, Model model){
        RetVal retVal = seckillFeignClient.getSeckillProductBySkuId(skuId);
        model.addAttribute("item",retVal.getData());
        return "seckill/detail";
    }
    //3.获取抢购码成功之后的页面
    @RequestMapping("/seckill-queue.html")
    public String seckillQueue(Long skuId,String seckillCode,Model model){
        model.addAttribute("skuId",skuId);
        model.addAttribute("seckillCode",seckillCode);
        return "seckill/queue";
    }

    //4.秒杀确认订单
    @RequestMapping("/seckill-confirm.html")
    public String seckillConfirm(Model model) {
        RetVal retVal = seckillFeignClient.seckillConfirm();
        if(retVal.isOk()){
            Map<String, Object>  data = (Map<String, Object>)retVal.getData();
            model.addAllAttributes(data);
        }else {
            model.addAttribute("message",retVal.getMessage());
        }
      return "seckill/confirm";
    }
}
